package com.eagle.videoboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eagle.videoboot.domain.entity.Comments;
import com.eagle.videoboot.domain.entity.SearchRecords;
import com.eagle.videoboot.domain.entity.Videos;
import com.eagle.videoboot.domain.vo.CommentsVO;
import com.eagle.videoboot.domain.vo.VideosVO;
import com.eagle.videoboot.mapper.CommentsCustomMapper;
import com.eagle.videoboot.mapper.VideosCustomMapper;
import com.eagle.videoboot.mapper.VideosMapper;
import com.eagle.videoboot.service.ICommentsService;
import com.eagle.videoboot.service.ISearchRecordsService;
import com.eagle.videoboot.service.IVideosService;
import com.eagle.videoboot.utils.PagedResult;
import com.eagle.videoboot.utils.TimeAgoUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 视频信息表 服务实现类
 * </p>
 *
 * @author eagle
 * @since 2020-04-22
 */
@Service
public class VideosServiceImpl extends ServiceImpl<VideosMapper, Videos> implements IVideosService {

	@Autowired(required = false)
	VideosCustomMapper videosCustomMapper;

	@Autowired(required = false)
	CommentsCustomMapper commentsCustomMapper;

	@Autowired(required = false)
	ISearchRecordsService usersReportServiceImpl;

	@Autowired(required = false)
	ICommentsService commentsServiceImpl;

	@Autowired(required = false)
	private Sid sid;

//	@Transactional(propagation = Propagation.REQUIRED)
//	@Override
//	public String saveVideo(Videos video) {
//
//		String id = sid.nextShort();
//		video.setId(id);
//		videosMapper.insertSelective(video);
//
//		return id;
//	}
//
//	@Transactional(propagation = Propagation.REQUIRED)
//	@Override
//	public void updateVideo(String videoId, String coverPath) {
//
//		Videos video = new Videos();
//		video.setId(videoId);
//		video.setCoverPath(coverPath);
//		videosMapper.updateByPrimaryKeySelective(video);
//	}

	//上下拉动，搜索调这个接口
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public PagedResult getAllVideos(Videos video, Integer isSaveRecord,
									Integer pageNo, Integer pageSize) {

		// 保存热搜词
		String desc = video.getVideoDesc();
		String userId = video.getUserId();
		if (isSaveRecord != null && isSaveRecord == 1) {
			SearchRecords record = new SearchRecords();
			String recordId = sid.nextShort();
			record.setId(recordId);
			record.setContent(desc);
			usersReportServiceImpl.save(record);
		}

		PageHelper.startPage(pageNo, pageSize);
		List<VideosVO> list = videosCustomMapper.queryAllVideos(desc, userId);
		PageInfo<VideosVO> pageList = new PageInfo<>(list); //分好页

		PagedResult pagedResult = new PagedResult();
		pagedResult.setPage(pageNo);
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setRecords(pageList.getTotal());

		return pagedResult;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult queryMyLikeVideos(String userId, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<VideosVO> list = videosCustomMapper.queryMyLikeVideos(userId);
		PageInfo<VideosVO> pageList = new PageInfo<>(list);

		PagedResult pagedResult = new PagedResult();
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setPage(page);
		pagedResult.setRecords(pageList.getTotal());

		return pagedResult;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult queryMyFollowVideos(String userId, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<VideosVO> list = videosCustomMapper.queryMyFollowVideos(userId);

		PageInfo<VideosVO> pageList = new PageInfo<>(list);

		PagedResult pagedResult = new PagedResult();
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setPage(page);
		pagedResult.setRecords(pageList.getTotal());

		return pagedResult;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public List<String> getHotwords() {
		return videosCustomMapper.getHotwords();
	}
//
//	@Transactional(propagation = Propagation.REQUIRED)
//	@Override
//	public void userLikeVideo(String userId, String videoId, String videoCreaterId) {
//		// 1. 保存用户和视频的喜欢点赞关联关系表
//		String likeId = sid.nextShort();
//		UsersLikeVideos ulv = new UsersLikeVideos();
//		ulv.setId(likeId);
//		ulv.setUserId(userId);
//		ulv.setVideoId(videoId);
//		usersLikeVideosMapper.insert(ulv);
//
//		// 2. 视频喜欢数量累加
//		videosMapperCustom.addVideoLikeCount(videoId);
//
//		// 3. 用户受喜欢数量的累加
//		usersMapper.addReceiveLikeCount(videoCreaterId);
//	}
//
//	@Transactional(propagation = Propagation.REQUIRED)
//	@Override
//	public void userUnLikeVideo(String userId, String videoId, String videoCreaterId) {
//		// 1. 删除用户和视频的喜欢点赞关联关系表
//
//		Example example = new Example(UsersLikeVideos.class);
//		Criteria criteria = example.createCriteria();
//
//		criteria.andEqualTo("userId", userId);
//		criteria.andEqualTo("videoId", videoId);
//
//		usersLikeVideosMapper.deleteByExample(example);
//
//		// 2. 视频喜欢数量累减
//		videosMapperCustom.reduceVideoLikeCount(videoId);
//
//		// 3. 用户受喜欢数量的累减
//		usersMapper.reduceReceiveLikeCount(videoCreaterId);
//
//	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void saveComment(Comments comment) {
		String id = sid.nextShort();
		comment.setId(id);
		comment.setCreateTime(LocalDateTime.now());

		this.commentsServiceImpl.save(comment);
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult getAllComments(String videoId, Integer page, Integer pageSize) {

		PageHelper.startPage(page, pageSize);
		List<CommentsVO> list = commentsCustomMapper.queryComments(videoId);

		for (CommentsVO c : list) {
			String timeAgo = TimeAgoUtils.format(c.getCreateTime());
			c.setTimeAgoStr(timeAgo);
		}

		PageInfo<CommentsVO> pageList = new PageInfo<>(list);

		PagedResult grid = new PagedResult();
		grid.setTotal(pageList.getPages());
		grid.setRows(list);
		grid.setPage(page);
		grid.setRecords(pageList.getTotal());

		return grid;
	}
}
